package com.exam.system.service.impl;

import com.exam.system.dao.ExamInfoDao;
import com.exam.system.entity.ExamInfo;
import com.exam.system.entity.ExamPaper;
import com.exam.system.entity.ExamType;
import com.exam.system.entity.ExamUser;
import com.exam.system.service.ExamInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description 用户service层
 * @versions 1.0
 */
@Service
@Transactional
public class ExamInfoServiceImpl implements ExamInfoService {
    @Autowired
    private ExamInfoDao examInfoDao;

    @Override
    public List<ExamInfo> findAll() {
        return examInfoDao.findAll();
    }

    @Override
    public List<ExamInfo> selectByTypeId(Integer examTypeId) {
        return examInfoDao.selectByTypeId(examTypeId);
    }

    @Override
    public List<ExamInfo> selectByExamName(ExamInfo examInfo) {
        return examInfoDao.selectByExamName(examInfo);
    }

    @Override
    public List<ExamInfo> selectByBeginTime(String beginTime) {
        return examInfoDao.selectByBeginTime(beginTime);
    }

    @Override
    public List<ExamInfo> selectByExamTypeId(ExamInfo examInfo) {
        return examInfoDao.selectByExamTypeId(examInfo);
    }

    @Override
    public ExamInfo selectByPrimaryKey(Integer id) {
        examInfoDao.selectByPrimaryKey(id);
        return null;
    }

    @Override
    public ExamInfo deleteById(Integer id) {
        examInfoDao.deleteById(id);
        return null;
    }

    @Override
    public ExamInfo insert(ExamInfo examInfo) {
        examInfo.setCreateTime(new Date());
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String fromDate = simpleFormat.format(  examInfo.getBeginTime());
        String toDate = simpleFormat.format( examInfo.getEndTime());
        examInfo.setIsdel("n");
        long from = 0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int min = (int) ((to - from)/(1000 * 60 ));
            examInfo.setDuration(min);
            examInfoDao.insertExam(examInfo);
        } catch (ParseException e) {
            e.printStackTrace();
        }
      //  examInfoDao.insertExam(examInfo);
        return null;
    }

    @Override
    public List<ExamPaper> findPaper() {
        return examInfoDao.findPaper();
    }

    @Override
    public List<ExamType> findExamType() {

        return examInfoDao.findExamType();
    }

    @Override
    public void updateById(ExamInfo examInfo) {
         examInfoDao.updateById(examInfo);
    }

    @Override
    public List<ExamUser> findAllUser() {
        return  examInfoDao.findAllUser();
    }
}
